package DataStructure.arrayAndList.list.listRealize;

import DataStructure.arrayAndList.list.Nodelj;

/**
 * @author liujun
 * @version 1.0
 * @date 2021/12/7
 * @author—Email ljfirst@mail.ustc.edu.cn
 * @blogURL https://blog.csdn.net/ljfirst
 * @description 链表合并
 * 给定两个升序链表，合并成一个升序链表
 */
public class ListBigIntegersMerge extends ListBigIntegersOperation {

    public Nodelj merge(Nodelj first, Nodelj second) {

        if (first == null || second == null) {
            return first == null ? second : first;
        }
        Nodelj result = null;
        Nodelj resultNext = null;
        while (first != null && second != null) {
            if (first.value > second.value) {
                resultNext = second;
                second = second.next;
            } else {
                resultNext = first;
                first = first.next;
            }
            if (result == null) {
                result = resultNext;
            } else {
                result.next = resultNext;
            }
        }
        if (first != null) {
            resultNext = first;
        }
        if (second != null) {
            resultNext = second;
        }
        return result;
    }
}
